(function () {
    var isReady = false;
    var readyList = [];
    var timer;
    ready = function (fn) {
        if (isReady || document.readyState === "interactive" || document.readyState === "complete")
            fn.call(document);
        else
            readyList.push(function () {
                return fn.call(this);
            });
        return this;
    };
    var onDOMReady = function () {
        for (var i = 0, n = readyList.length; i < n; i++)
            readyList[i].apply(document);
        readyList = [];
    };
    var bindReady = function (evt) {
        if (isReady) return;
        isReady = true;
        onDOMReady.call(window);
        if (document.removeEventListener) {
            document.removeEventListener("DOMContentLoaded", bindReady, false);
        } else if (document.attachEvent) {
            document.detachEvent("onreadystatechange", bindReady);
            if (window == window.top) {
                clearInterval(timer);
                timer = null;
            }
        }
    };
    if (document.addEventListener) {
        document.addEventListener("DOMContentLoaded", bindReady, false);
    } else if (document.attachEvent) {
        document.attachEvent("onreadystatechange", function () {
            if ((/loaded|complete/).test(document.readyState))
                bindReady();
        });
        if (window == window.top) {
            timer = setInterval(function () {
                try {
                    isReady || document.documentElement.doScroll('left');
                } catch (e) {
                    return;
                }
                bindReady();
            }, 5);
        }
    }
})();


var bszCaller = {
    fetch: function (url, callback) {
        var fn = 'BusuanziCallback_' + Math.floor(Math.random() * 1099511627776);
        window[fn] = this.evalCall(callback);
        url = url.replace('=BusuanziCallback', '=' + fn);

        scriptTag = document.createElement('SCRIPT');
        scriptTag.type = 'text/javascript';
        scriptTag.defer = true;
        scriptTag.src = url;
        document.getElementsByTagName('HEAD')[0].appendChild(scriptTag);
    },
    evalCall: function (callback) {
        return function (data) {
            ready(function () {
                try {
                    callback(data);
                    scriptTag.parentElement.removeChild(scriptTag);
                } catch (e) {
                    bszTag.hides();
                }
            });
        }
    }
};

bszCaller.fetch("//service.ibruce.info/busuanzi?jsonpCallback=BusuanziCallback", function (data) {
    bszTag.texts(data);
    bszTag.shows();
});

var bszTag = {
    bszs: ['site_pv', 'page_pv', 'site_uv'],
    texts: function (json) {
        this.bszs.map(function (bsz) {
            var bv = document.getElementById('busuanzi_value_' + bsz);
            if (bv) bv.innerHTML = json[bsz];
        });
    },
    hides: function () {
        this.bszs.map(function (bsz) {
            var bc = document.getElementById('busuanzi_container_' + bsz);
            if (bc) bc.style.display = 'none';
        });
    },
    shows: function () {
        this.bszs.map(function (bsz) {
            var bc = document.getElementById('busuanzi_container_' + bsz);
            if (bc) bc.style.display = 'inline';
        });
    }
};